package com.hdu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hdu.entity.Exam;
import com.hdu.pojo.Grade;
import com.hdu.pojo.Rank;
import com.hdu.pojo.Score;
import com.hdu.pojo.Table;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 考试实体类 Mapper 接口
 * </p>
 *
 * @author xiaobai
 * @since 2025-02-06
 */
public interface ExamMapper extends BaseMapper<Exam> {
    /**
     * 考试信息查询
     *
     * @return List<Exam>
     */
    List<Exam> list(String grade, String label, String start, String end, String name);

    /**
     * 考试详细信息查询
     *
     * @return List<Exam>
     */
    @Select("select * from exam where eID=#{id}")
    Exam getExam(String id);

    /**
     * 考试序列查询
     *
     * @return Integer
     */
    @Select("SELECT FIND_IN_SET(eID, (SELECT GROUP_CONCAT(eID ORDER BY eID DESC) FROM exam)) FROM exam where eID=#{mID}")
    Integer examRank(String mID);

    /**
     * 考试前一次考试查询
     *
     * @return List<Exam>
     */
    @Select("select eID from exam order by eID desc limit ${rank},1")
    String getExamID(Integer rank);

    /**
     * 考试前一次考试查询
     *
     * @return List<Rank>
     */
    List<Rank> classGrade(String sID, String cID, String mID, String eID, String subject);

    /**
     * 考试详细信息查询
     *
     * @return List<String>
     */
    @Select("select cID from class where cID like concat(#{name}, '%')")
    List<String> listClass(String id);

    /**
     * 考试总体成绩信息查询
     *
     * @return List<Score>
     */
    List<Score> wholeGrade(String sID, String name, String prop, String order, String cID, String mID);

    /**
     * 考试总体成绩信息查询
     *
     * @return List<Table>
     */
    List<Table> examTable(String cID, String mID, String subject, String name, Integer score);

    /**
     * 考试成绩平均分查询
     *
     * @return Grade
     */
    Grade averageGrade(String cID, String mID);

    /**
     * 年级最高分查询
     *
     * @return Double
     */
    @Select("select max(${subject}) from vgrade where mID=#{mID}")
    Double topScore(String mID, String subject);

    /**
     * 年级平均分查询
     *
     * @return Double
     */
    @Select("select round(avg(${subject}),2)  from vgrade where mID=#{mID}")
    Double avgScore(String mID, String subject);

    /**
     * 班级成绩分段查询
     *
     * @return List<Rank>
     */
    @Select("select name,${subject} as score from vrank where cID=#{cID} and mID=#{mID} order by score desc")
    List<Rank> listLabel(String subject, String cID, String mID);
}
